var userController=require('../controllers/userController.js');
var commentController=require('../controllers/CommentController.js');
var WxController=require('../controllers/wxController.js');
module.exports = function(app) {
    var bind = function (accepter, method) {
        return accepter[method].bind(accepter);
    };
    app.get('/',bind(userController,'index'));

    app.get('/reg', checkNotLogin);
    app.get('/reg',bind(userController,'reg'));
    app.post('/reg', checkNotLogin);
    app.post('/reg', bind(userController,'post_reg'));

    app.get('/login', checkNotLogin);
    app.get('/login', bind(userController,'get_login'));

    app.post('/login', checkNotLogin);
    app.post('/login', bind(userController,'post_login'));

    app.get('/post', checkLogin);
    app.get('/post', bind(userController,'post'));

    app.post('/post', checkLogin);
    app.post('/post_content',bind(userController,'post_content'));

    app.get('/logout', checkLogin);
    app.get('/logout', bind(userController,'logout'));
    app.get('/comment',bind(commentController,'comment'));
    app.post('/post_comment',bind(commentController,'post_comment'));
    app.get('/in_page',bind(WxController,'in_page'));
    app.post('/post_score',bind(WxController,'post_score'));
    function checkLogin(req, res, next) {
        if (!req.session.user) {
            req.flash('error', '未登录!');
            res.redirect('/login');
        }
        next();
    }

    function checkNotLogin(req, res, next) {
        if (req.session.user) {
            req.flash('error', '已登录!');
            res.redirect('back');
        }
        next();
    }
};